﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using JSShop.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using wnxd.javascript;

    public class SKUSelector_3q : WebControl
    {
        public const string TagID = "SKUSelector_3q";

        public SKUSelector_3q()
        {
            base.ID = "SKUSelector_3q";
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DataSource != null && this.DataSource.Count > 0)
            {
                StringBuilder builder = new StringBuilder();
                IList<string> list = new List<string>();
                builder.AppendFormat("<input type=\"hidden\" id=\"{0}\" />", "hiddenSkuId");
                dynamic wjson = new json(new object[0]);
                int i = 0;
                foreach (SKUItemInfo item in this.DataSource)
                {
                    int index = 0;
                    for (; index < wjson.length; index++)
                    {
                        string skuid = wjson[index].skuid;
                        if (skuid == item.SkuId)
                        {
                            break;
                        }
                    }
                    if (index == wjson.length)
                    {
                        index = wjson.push(new { skuid = item.SkuId, index = new object[] { }, info = new { pid = item.ProductId } });
                    }
                    wjson[index][item.AttributeId.ToString()] = new json(item.ValueId);
                }
                foreach (SKUItemInfo item in this.DataSource)
                {
                    if (!list.Contains(item.AttributeName))
                    {
                        list.Add(item.AttributeName);
                        builder.Append("<div class=\"details-option-item clf\">");
                        builder.AppendFormat("<label class=\"fl\">{0}：</label><input type=\"hidden\" name=\"skuCountname\" AttributeName=\"{0}\" id=\"skuContent_{1}\" />", item.AttributeName, item.AttributeId);
                        builder.Append("<ul class=\"details-option-column clf\">");
                        IList<string> list2 = new List<string>();
                        foreach (SKUItemInfo item2 in this.DataSource)
                        {
                            if ((string.Compare(item.AttributeName, item2.AttributeName) == 0) && !list2.Contains(item2.ValueStr))
                            {
                                for (int m = 0; m < wjson.length; m++)
                                {
                                    if (wjson[m][item2.AttributeId.ToString()].ToString() == item2.ValueId.ToString())
                                    {
                                        wjson[m]["index"].push(i);
                                    }
                                }
                                i++;
                                string str2 = string.Concat(new object[] { "skuValueId_", item2.AttributeId, "_", item2.ValueId });
                                list2.Add(item2.ValueStr);
                                builder.AppendFormat("<li id=\"{0}\" AttributeId=\"{1}\" ValueId=\"{2}\" ValueStr=\"{3}\"><a href=\"javascript:void(0);\">{3}</a></li>", str2, item2.AttributeId, item2.ValueId, item2.ValueStr);
                            }
                        }
                        builder.AppendLine("</ul></div>");
                    }
                }
                builder.Append("<script type=\"text/javascript\">");
                builder.AppendFormat("var selectInfo = {0};", wjson.ToString());
                for (int n = 0; n < wjson.length; n++)
                {
                    if (wjson[n]["info"]["pid"].ToString() == this.ProductId.ToString())
                    {
                        builder.AppendFormat("var nowIndex = {0};", wjson[n]["index"]);
                        builder.AppendFormat("var nowPid = {0};", this.ProductId);
                        break;
                    }
                }
                builder.Append("</script>");
                writer.Write(builder.ToString());
            }
        }
        public List<SKUItemInfo> DataSource { get; set; }

        public override string ID
        {
            get
            {
                return base.ID;
            }
            set
            {
            }
        }
        public int ProductId { get; set; }
    }
}

